package cn.xzqwjw.taskmanager.service.impl;

import cn.xzqwjw.taskmanager.domain.pojo.SysRolePurview;
import cn.xzqwjw.taskmanager.domain.vo.ResponseVo;
import cn.xzqwjw.taskmanager.repository.SysRolePurviewMapper;
import cn.xzqwjw.taskmanager.service.SysRolePurviewService;
import cn.xzqwjw.taskmanager.utils.CommonUtil;
import cn.xzqwjw.taskmanager.utils.JsonUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author rush
 */
@Service("SysRolePurviewService")
@Transactional(rollbackFor = Exception.class)
public class SysRolePurviewServiceImpl extends ServiceImpl<SysRolePurviewMapper, SysRolePurview>
    implements SysRolePurviewService {

  @Override
  public String getRoleIdsByPurviewId(Integer idPurview) {
    CommonUtil.checkId(idPurview);
    QueryWrapper<SysRolePurview> wrapper = new QueryWrapper<>();
    wrapper.eq("id_purview", idPurview);
    List<SysRolePurview> listRolePurview = list(wrapper);
    List<Integer> listId = new ArrayList<>();
    for (SysRolePurview sysRolePurview : listRolePurview) {
      listId.add(sysRolePurview.getIdRole());
    }
    return JsonUtil.obj2Json(ResponseVo.success(listId.toArray(new Integer[0])));
  }

  @Override
  public String getPurviewIdsByRoleId(Integer idRole) {
    CommonUtil.checkId(idRole);
    QueryWrapper<SysRolePurview> wrapper = new QueryWrapper<>();
    wrapper.eq("id_role", idRole);
    List<SysRolePurview> listRolePurview = list(wrapper);
    List<Integer> listId = new ArrayList<>();
    for (SysRolePurview sysRolePurview : listRolePurview) {
      listId.add(sysRolePurview.getIdPurview());
    }
    return JsonUtil.obj2Json(ResponseVo.success(listId.toArray(new Integer[0])));
  }

}
